package com.dup.jxlh.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dup.jxlh.admin.entities.auth.AuthUser;
import com.dup.jxlh.admin.service.OwnValidatorService;
import com.dup.jxlh.api.entities.PaginationData;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.math.BigInteger;

@Service("ownValidatorServiceImpl")
public class OwnValidatorServiceImpl implements OwnValidatorService {

    @Override
    public Boolean byCompanyIdValidator(BigInteger id) {
        if (getAuthUser().getCompanyId().equals(id)) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean byLevelValidator(BigInteger id1, BigInteger id2, BigInteger id3) {
        Integer level = getAuthUser().getLevel();
        if (level.equals(1)) {
            return byCompanyIdValidator(id1);
        }
        if (level.equals(2)) {
            return byDepartmentIdValidator(id2);
        }
        if (level.equals(3)) {
            return byAdminIdValidator(id3);
        }
        return false;
    }

    @Override
    public Boolean byLevelValidator(BigInteger id1, BigInteger id2, BigInteger id3, BigInteger id4) {
        Integer level = getAuthUser().getLevel();
        if (level.equals(1)) {
            return byCompanyIdValidator(id1);
        }
        if (level.equals(2)) {
            return byDepartmentIdValidator(id2);
        }
        if (level.equals(3)) {
            return byAdminIdValidator(id3) || byAdminIdValidator(id4);
        }
        return false;
    }

    @Override
    public void setQueryWrapper(QueryWrapper queryWrapper, PaginationData paginationData) {
        if ("descending".equalsIgnoreCase(paginationData.getRule())) {
            queryWrapper.orderByDesc(paginationData.getField());
        } else if ("ascending".equalsIgnoreCase(paginationData.getRule())) {
            queryWrapper.orderByAsc(paginationData.getField());
        } else {
            queryWrapper.orderByDesc("id");
        }
    }

    @Override
    public void setQueryWrapperByCompany(QueryWrapper queryWrapper, PaginationData paginationData) {
        queryWrapper.eq("company_id", getAuthUser().getCompanyId());
        setQueryWrapper(queryWrapper, paginationData);
    }

    @Override
    public void setQueryWrapperByLevel(QueryWrapper queryWrapper, PaginationData paginationData) {
        setQueryWrapperByCompany(queryWrapper, paginationData);
        AuthUser authUser = getAuthUser();
        Integer level = authUser.getLevel();
        if (!level.equals(1)) {
            queryWrapper.eq("department_id", authUser.getDepartmentId());
        }
        if (level.equals(3)) {
            queryWrapper.eq("record_id", authUser.getId());
        }
    }

    @Override
    public void setChapterQueryWrapperByLevel(QueryWrapper queryWrapper, PaginationData paginationData) {
        setQueryWrapperByCompany(queryWrapper, paginationData);
        AuthUser authUser = getAuthUser();
        Integer level = authUser.getLevel();
        if (!level.equals(1)) {
            queryWrapper.eq("department_id", authUser.getDepartmentId());
        }
        if (level.equals(3)) {
            queryWrapper.apply("(apply_id = {0} or check_id = {0})", authUser.getId());
        }
    }

    private AuthUser getAuthUser() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        return (AuthUser) auth.getPrincipal();
    }

    private Boolean byDepartmentIdValidator(BigInteger id) {
        if (getAuthUser().getDepartmentId().equals(id)) {
            return true;
        }
        return false;
    }

    private Boolean byAdminIdValidator(BigInteger id) {
        if (getAuthUser().getId().equals(id)) {
            return true;
        }
        return false;
    }
}
